﻿// ---------------------------------------------------------------------
// Lớp Card_API
//
// Nội dung:
//    + Các hàm API xử lý player 
//
// Copyright (c) 2009 YugiOH 
//
// Nguyễn Hữu Lê Trọng Tín      YM: ilu2009             ĐT: 0906520611
// Võ Phước Hậu                 YM : phuochauspkt       ĐT: 0979202707
// ---------------------------------------------------------------------


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ServerYuGi;
using Common;
using Yugi.Client.Card;
using ServerYuGi.Block;

namespace APIEffect.PLAYER
{
    public class PLAYER_API
    {
        SSupervisor super;
        public PLAYER_API(SSupervisor super)
        {
            this.super = super;
        }
        //-----------------------------Health-------------------------------------//
        public void PLAYER_Health_Increase(int ID, int DiemCong)
        {
            SPlayer player = super.LayPlayer(ID);
            player.SoDiem += DiemCong;
            List<byte> tmp = new List<byte>();
            tmp.Add((byte)ID);
            tmp.AddRange(BitConverter.GetBytes(player.SoDiem));
            super.SendMessage_PLAYER_THAYDOIDIEM_ToAll(tmp.ToArray());
            
                
        }
        public void PLAYER_Health_Decrease(int ID, int DiemGiam)
        {
            SPlayer player = super.LayPlayer(ID);
            player.SoDiem -= DiemGiam;
            List<byte> tmp = new List<byte>();
            tmp.Add((byte)ID);
            tmp.AddRange(BitConverter.GetBytes(player.SoDiem));
            super.SendMessage_PLAYER_THAYDOIDIEM_ToAll(tmp.ToArray());
            
            // 16/09/2010, Hau fix, kiem tra xem player sau khi giam diem co bi tru het diem hay khong
            super.KiemTraHetDiem(); 
            
        }
        public int PLAYER_GetHealth(int id)
        {
            SPlayer player = super.LayPlayer(id);
            return player.SoDiem;
        }
        //------------------------------BF Role------------------------------------//

        public int PLAYER_BF_GetAttacker()
        {
            return super.TurnManager.IDPlayerTrongLuot;
        }
        public int PLAYER_BF_GetDefender()
        {
            return super.LayPlayerKhacVoiPlayerDangTrongLuot().ID;
        }
        public int PLAYER_GetAnotherPlayer(int player_id)
        {
            return super.LayPlayerKhacVoiID(player_id).ID;
        }
        public int PLAYER_GetCurrentChecker()
        {
            return super.ExEffectManager.IDPlayerCheck;
        }
        //------------------------------Card--------------------------------------//
        public YBaseCard PLAYER_Find_Card(int player_id, int id)
        {
            SCard temp=super.LayPlayer(player_id).LayCardInAll(id);
            if(temp!=null)
                return temp.MyYBaseCard;
            return null;
        }

        public List<SCard> PLAYER_Get_ListCard_Hand(int player_id)
        {
            return new List<SCard>(super.LayPlayer(player_id).SoBaiTrenTay);
        }

        public List<SCard> PLAYER_Get_ListCard_Table(int player_id)
        {
            return new List<SCard>(super.LayPlayer(player_id).SoBaiTrenBan);
        }

        public List<SCard> PLAYER_Get_ListCard_Deck(int player_id)
        {
            return new List<SCard>(super.LayPlayer(player_id).SoBaiChuaChia);
        }

        public List<SCard> PLAYER_Get_ListCard_Graveyard(int player_id)
        {
            return new List<SCard>(super.LayPlayer(player_id).SoBaiDaChet);
        }

        public int PLAYER_GetNumberMonster_Table(int player_id)
        {
            return super.LayPlayer(player_id).SoSummonTrenBan;
        }

        public int PLAYER_GetNumberSpellTrap_Table(int player_id)
        {
            return super.LayPlayer(player_id).SoTrapHoacSpellTrenBan;
        }

        public void PLAYER_Discard_Card(int player_id, int id)
        {
            SPlayer player = super.LayPlayer(player_id);
            if(player==null)
                return;
            int vitri;
            int idcard;
            if (id == -1)
            {
                idcard = player.GetRandomID_InHand();
                super.logManager.LogAPIDiscard(player.ID, idcard);
            }
            else
                idcard = id;

            SCard card = player.LayCardTrenTay(idcard);
            vitri = player.GetIndexCard_InHand(id);
            super.SendMessage_CARD_XOABAI_TAY_ToID(player_id, id, vitri);
            player.MoveFromHand2Graveyard(idcard);
            super.SendMessage_CARD_MOVETO_GRAVEYARD_ToAll(player_id, card.MyYBaseCard.Card_TypeID, idcard);
        }

        public void PLAYER_Draw_Card(int player_id)
        {
            SPlayer player = super.LayPlayer(player_id);
            SCard card = player.RutLaBai();               
            super.SendMessage_API_DRAWCARD_ToAll(player_id, card.MyYBaseCard.ID, card.MyYBaseCard.Card_TypeID);
            super.logManager.LogAPIRutBai(player_id, card.MyYBaseCard.ID);
        }

        public void PlAYER_ChangeControl_Card_Table2Table(int idplayer, int idCard)
        {
            SPlayer player = super.LayPlayer(idplayer);
            SPlayer Other = super.LayPlayerKhacVoiID(idplayer);
            SCard card = player.LayCardTrenBan(idCard);
            if (card == null)
                return;
            Other.SoBaiTrenBan.Add(card);
            Other.Block.DuocPhep_TanCong.Add(card.MyYBaseCard.ID);
            player.SoBaiTrenBan.Remove(card);
            super.SendMessage_API_CHANGECONTROL_ToAll(idplayer, idCard, Other.ID, Zone.TABLE, Zone.TABLE);
        }

        //------------------------------Block---------------------------------
        public BlockData PLAYER_Get_Block(int idplayer)
        {
            SPlayer player = super.LayPlayer(idplayer);
            return new BlockData(player.Block);
        }
        public void PLAYER_Update_NormalSummon(int idplayer, bool value)
        {
            SPlayer player = super.LayPlayer(idplayer);
            player.Block.DuocPhep_RaBaiNormal = value;
            super.SendMessage_TURN_MF1_BLOCK_ToID(idplayer);
        }
        public void PLAYER_Update_AllowMonsterActive(int idplayer, bool value)
        {
            SPlayer player = super.LayPlayer(idplayer);
            player.Block.DuocPhep_ActiveMonster = value;
            super.SendMessage_TURN_MF1_BLOCK_ToID(idplayer);
        }

    }
}
